﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace KLTN
{
    
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SpringPendulum _SpringPendulum;
        Pendulum _Pendulum;
        
        Texture2D bt_control_click;
        Texture2D bt_control_release;

        Vector2 vtCLick;
        Vector2 vtRelease;

        bool click = false;
        bool createform = false;

        MouseState mouse;
        MouseState mouseold;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        
        protected override void Initialize()
        {
            this.IsMouseVisible = true;       
            base.Initialize();
        }

      
        protected override void LoadContent()
        {
           
            spriteBatch = new SpriteBatch(GraphicsDevice);

            #region Con Lac Lo Xo           
            CModel SPModel = new CModel(Content.Load<Model>("Models\\cau"), new Vector3(1000, 1000, 1000), Vector3.Zero, new Vector3(100), GraphicsDevice);
            _SpringPendulum = new SpringPendulum(SPModel, 600, 6, 0, new Vector3(-1000, 0, 0));
            _SpringPendulum.LoadContent(Content, GraphicsDevice);
            #endregion

            #region Con Lac Don
            CModel PenModel = new CModel(Content.Load<Model>("Models\\tru2"), new Vector3(1000, 1000, 1000), Vector3.Zero, new Vector3(100), GraphicsDevice);
            _Pendulum = new Pendulum(PenModel, MathHelper.ToRadians(45),MathHelper.ToRadians(-90),2,new Vector3(0,0,2000));
            _Pendulum.LoadContent(Content, GraphicsDevice);
            #endregion
            #region Menu     
            spriteBatch = new SpriteBatch(GraphicsDevice);
            bt_control_click = Content.Load<Texture2D>("Pictures\\menu\\bt_control_click");
            bt_control_release = Content.Load<Texture2D>("Pictures\\menu\\bt_control_release");
            vtRelease = new Vector2((Window.ClientBounds.Width - bt_control_release.Width), (Window.ClientBounds.Height / 2 - bt_control_release.Height/2));
            vtCLick = new Vector2(vtRelease.X, vtRelease.Y);
            #endregion
        }

       
        protected override void UnloadContent()
        {
           
        }

       
        protected override void Update(GameTime gameTime)
        {
           
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            _SpringPendulum.Update(gameTime);
            _Pendulum.Update(gameTime);
            #region Menu
            mouse = Mouse.GetState();
            mouseold = mouse;
            float width = bt_control_release.Width;
            float heigh = bt_control_release.Width;
            float vtRX = vtRelease.X;
            float vtRY = vtRelease.Y;

            if (vitrichuot(vtRX, vtRY, width, heigh) == true && mouse.LeftButton == ButtonState.Pressed)
            {
                click = true;
                bt_ControlClick();
                
            }
            if (vitrichuot(vtRX, vtRY, width, heigh) == false && mouse.LeftButton == ButtonState.Released && click == true)
            {
                click = false;
            }
            base.Update(gameTime);
            #endregion
            base.Update(gameTime);
        }

       
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            #region ResetGraphic
            ResetGraphic();
            #endregion

            #region 3D
            BeginRender3D();
            _SpringPendulum.Draw();
            _Pendulum.Draw();
            #endregion

            #region 2D
            #region Menu
            float width = bt_control_release.Width;
            float heigh = bt_control_release.Height;
            float vtRX = vtRelease.X;
            float vtRY = vtRelease.Y;

            if ((vitrichuot(vtRX, vtRY, width, heigh) == true && click == true))
            {
                click = false;
                drawrealse();
            }
            else
            {
                if (vitrichuot(vtRX, vtRY, width, heigh) == true)
                {
                    drawclick();
                }
                else
                    drawrealse();
            }
            #endregion
            #endregion

            base.Draw(gameTime);
        }

        public void ResetGraphic()
        {              
            GraphicsDevice.BlendState = BlendState.AlphaBlend;
            GraphicsDevice.DepthStencilState = DepthStencilState.None;
            GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
            GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;
        }
        public void BeginRender3D()
        {
            GraphicsDevice.BlendState = BlendState.Opaque;
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
        }
        public void drawclick()
        {
            spriteBatch.Begin();
            spriteBatch.Draw(bt_control_click, vtCLick, Color.White);
            spriteBatch.End();
        }
        public void drawrealse()
        {
            spriteBatch.Begin();
            spriteBatch.Draw(bt_control_release, vtCLick, Color.White);
            spriteBatch.End();
        }

        public bool vitrichuot(float vtRX, float vtRY, float width, float heigh)
        {
            if (mouse.X >= vtRX && mouse.Y >= vtRY && mouse.X <= (vtRX + width) && mouse.Y <= (vtRY + heigh))
                return true;
            else
                return false;
        }

        //llấy giá trị từ form
        public void Getvalue(float giatri1,float giatri2,float giatri3)
        {
            
        }
        //sự kiện click chuột nút control
        public void bt_ControlClick()
        {
            Form_Control.daodongdieuhoa fr = new Form_Control.daodongdieuhoa();
            fr.FormClosed += new System.Windows.Forms.FormClosedEventHandler(fr_FormClosed);
            if (createform == false)
            {
                fr.Mygetdata = new Form_Control.daodongdieuhoa.GetPara(Getvalue);
                fr.Show();               
                createform = true;
            }
        }
        void fr_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
        {
            createform = false;
        }
    }
}
